package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.pojo.Address;
import com.zjitc.pojo.Car;
import com.zjitc.pojo.Caritem;
import com.zjitc.pojo.User;
import com.zjitc.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Create By IntelliJ IDEA
 *
 * @author: myq
 * @create-Time: 2017/11/20  16:59
 */
@Controller
@RequestMapping("/car")
public class CarController extends BaseController {

    @Autowired
    private CarService service;

    private static final String USER = "user";

    @RequestMapping(value = "/add2cart", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse cart(HttpSession session, String pid,
                               @RequestParam(required = false, defaultValue = "1") Integer count)
            throws ServletException, IOException {
        User user = getSessionUser(session);
        if (user == null) {
            return ServerResponse.createError("user is No login");
        } else {
            service.add2car(user.getUid(), count, pid);
            return ServerResponse.createSuccess();
        }
    }

    @RequestMapping("/showcar")
    public ModelAndView show(HttpSession session) throws ServletException, IOException {
        User user = getSessionUser(session);
        ModelAndView mv = new ModelAndView();
        if (user == null) {
            mv.setViewName("user/login");
            return mv;
        }
        mv.setViewName("cart/cart");
        List<Caritem> items = service.findAll(user.getUid());
        Float total = 0f;
        for (Caritem caritem : items) {
            total += caritem.getPrice();
        }

        mv.addObject("items", items);
        mv.addObject("total", total);

        return mv;
    }

    @RequestMapping("/deletecar")
    @ResponseBody
    public boolean delete(HttpSession session) throws ServletException, IOException {
        User user = getSessionUser(session);
        int success = service.deleteAll(user.getUid());
        return success > 0 ;
    }

    @RequestMapping("/deletesingle")
    @ResponseBody
    public ServerResponse<Car> deletesingle(@RequestParam("uid") String uid,
                                            @RequestParam("pid") String pid) throws ServletException, IOException {
        return service.deletesingle(pid, uid);
    }


    @RequestMapping("/goCheckoutCart")
    public ModelAndView gocheckOutCart(HttpSession session) {
        User user = getSessionUser(session);
        if(user == null){
            ModelAndView mv = new ModelAndView("user/login");
            return mv;
        }
        ModelAndView mv = new ModelAndView("order/order_info");
        List<Caritem> items = service.findAll(user.getUid());
        Float total = 0f;
        for (Caritem caritem : items) {
            total += caritem.getPrice();
        }

        List<Address> addresses = service.findAllMyAddress(user.getUid());
        mv.addObject("addresses", addresses);
        mv.addObject("items", items);
        mv.addObject("total", total);

        return mv;
    }


    @RequestMapping(value = "/saveaddress", method = RequestMethod.POST)
    @ResponseBody
    public String saveaddress(Address address, HttpSession session) {
        User user = getSessionUser(session);
        return service.addAddress(address, user);
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(Address address, HttpSession session) {
        User user = getSessionUser(session);
        address.setUid(user.getUid());
        return service.updateAddress(address);
    }
}
